/*

Part 2 of the analysis code begins here. At this point, it is possible to follow without
having access to the raw individual level data. To run, make sure that all the
data files uploaded to the Dataverse have been downloaded (finaldata.dta, matrices.dta,
BES_est.dta DES_est.dta, NES_est.dta and VU_est.dta), and change the cd command below to
the folder where they are stored on your computer.

*/

* CHANGE DIRECTORY HERE
cd "C:\Userdata\Shared\Dofiles\PrelDoFiles\PrelRafael\dataverse_files"


/*
The program calc_SE calculates the standard error of the meta-analytical
effect average (corresponds to eq. 7 in the main manuscript).

Takes the following arguments:
1. Predictor variable (string)
2. Matrix model slot to use (int)
3. Number of outcomes to include (int)
4. Flip vector prefix (string). This is used to indicate which dataset estimates
	are coming from (matrix names have different prefixes).
*/
capture program drop calc_SE
program define calc_SE

	local indepvar = "`1'"
	local model = `2'

	* Total sum of variances
	qui: egen sum_var_`indepvar'_`model'=sum(mat_`indepvar'_var`model')
	
	* Second term
	local tempsum = 0
	local actualn = 0
	forvalues i=1(1)`3' {
		if mat_`indepvar'_se`model'[`i']!=. {
			local actualn = `actualn'+1
		}
		forvalues j=1(1)`3' {
			if `i'!=`j' & mat_`indepvar'_se`model'[`i']!=. & mat_`indepvar'_se`model'[`j']!=. {
				local tempsum = `tempsum' + `4'corr_mat[`i',`j'] * mat_`indepvar'_se`model'[`i'] * ///
					mat_`indepvar'_se`model'[`j'] * `4'flip_`indepvar'[`i']  * `4'flip_`indepvar'[`j']
			}
		}
	}
	
	* Total standard error
	qui:gen se_`indepvar'_`model' = sqrt(`tempsum' + sum_var_`indepvar'_`model'[1])*(1/`actualn') in 1

end


**************************
* LOAD DATA AND MATRICES *
**************************

* First, load the necessary correlation matrices from step 1
use "matrices.dta", clear
local matrices = "corr_mat n_corr_mat VU_n_corr_mat VU_corr_mat BES_n_corr_mat BES_corr_mat DES_n_corr_mat DES_corr_mat NES_n_corr_mat NES_corr_mat ROBUST_e_corr_mat ROBUST_corr_mat"
foreach m in `matrices' {
	mkmat `m'*, matrix(`m')
}


use "finaldata.dta", clear
*use "finaldata_alt.dta", clear // Uncomment this if Table 6 from Appendix B (table_main_alt.tex) is to be produced
cd "output"



/*
In this section we take the dataset stored before and perform the following steps:
1. Do all renormalization (to positive coefficients) required for model comparisons.
2. Calculate means and standard errors for the meta-analyzed models.
3. Calculate model comparison (delta) stats.
*/
local allindepvars = "bin_edu extraversion_SALTY LOC_SALTY risk_preference education_years wealth_IHS wealth_net_IHS income10_tr trust antisocial altruism utilitarian stdIQ"
foreach indepvar in `allindepvars' {

	* Generate empty model flip vector. This is used for transforming to positive
	* coefficients in the renormalized models based on the empty model results.
	qui:gen flip_`indepvar' = 1
	qui:replace flip_`indepvar' = -1 if mat_`indepvar'_beta1<0
	
	* Generate naive model flip vector. This is used for transforming to positive
	* coefficients in the renormalized models based on the naive model results.
	qui:gen n_flip_`indepvar' = 1
	qui:replace n_flip_`indepvar' = -1 if mat_`indepvar'_beta2<0
	
	* Flipped betas
	qui:gen `indepvar'_1 = mat_`indepvar'_beta1 * flip_`indepvar'
	qui:gen `indepvar'_2 = mat_`indepvar'_beta2 * flip_`indepvar'
	qui:gen `indepvar'_3 = mat_`indepvar'_beta3 * flip_`indepvar'
	qui:gen `indepvar'_7 = mat_`indepvar'_beta7 * flip_`indepvar'
	qui:gen `indepvar'_8 = mat_`indepvar'_beta8 * flip_`indepvar'
	qui:gen `indepvar'_9 = mat_`indepvar'_beta9 * flip_`indepvar'
	qui:gen `indepvar'_10 = mat_`indepvar'_beta10 * flip_`indepvar'
	qui:gen `indepvar'_11 = mat_`indepvar'_beta11 * flip_`indepvar'
	qui:gen `indepvar'_12 = mat_`indepvar'_beta12 * flip_`indepvar'
	qui:gen `indepvar'_13 = mat_`indepvar'_beta13 * flip_`indepvar'
	qui:gen `indepvar'_14 = mat_`indepvar'_beta14 * flip_`indepvar'
	qui:gen `indepvar'_15 = mat_`indepvar'_beta15 * flip_`indepvar'
	qui:gen `indepvar'_16 = mat_`indepvar'_beta16 * flip_`indepvar'
	
	* Generate copies for naive renorming (se, var, n)
	qui:replace mat_`indepvar'_se28 = mat_`indepvar'_se2
	qui:replace mat_`indepvar'_se29 = mat_`indepvar'_se3
	qui:replace mat_`indepvar'_var28 = mat_`indepvar'_var2
	qui:replace mat_`indepvar'_var29 = mat_`indepvar'_var3
	qui:replace mat_`indepvar'_n28 = mat_`indepvar'_n2
	qui:replace mat_`indepvar'_n29 = mat_`indepvar'_n3
	
	* Naive flipped betas
	qui:gen `indepvar'_28 = mat_`indepvar'_beta28 * n_flip_`indepvar'
	qui:gen `indepvar'_29 = mat_`indepvar'_beta29 * n_flip_`indepvar'
	qui:gen `indepvar'_32 = mat_`indepvar'_beta32 * n_flip_`indepvar'
	qui:gen `indepvar'_33 = mat_`indepvar'_beta33 * n_flip_`indepvar'
	qui:gen `indepvar'_34 = mat_`indepvar'_beta34 * n_flip_`indepvar'
	qui:gen `indepvar'_35 = mat_`indepvar'_beta35 * n_flip_`indepvar'

	* Per model runs
	foreach i in 1 2 3 7 8 9 10 11 12 13 14 15 16 {
	
		* Significant model estimate?
		qui:gen mat_`indepvar'_sig`i'=0
		qui:replace mat_`indepvar'_sig`i'=1 if mat_`indepvar'_p`i'<=0.05
		
		* Effect SE per model
		calc_SE `indepvar' `i' 34 ""
	}
	
	* Naive renormed SE's
	calc_SE `indepvar' 28 34 "n_"
	calc_SE `indepvar' 29 34 "n_"
	calc_SE `indepvar' 32 34 "n_"
	calc_SE `indepvar' 33 34 "n_"
	calc_SE `indepvar' 34 34 "n_"
	calc_SE `indepvar' 35 34 "n_"
	
	* VU comparison models
	qui:gen VU_flip_`indepvar' = 1
	qui:replace VU_flip_`indepvar' = -1 if mat_`indepvar'_beta4<0
	qui:gen `indepvar'_4 = abs(mat_`indepvar'_beta4)
	qui:gen `indepvar'_5 = mat_`indepvar'_beta5*VU_flip_`indepvar'
	qui:gen `indepvar'_6 = mat_`indepvar'_beta6*VU_flip_`indepvar'
	calc_SE `indepvar' 4 21 "VU_"
	calc_SE `indepvar' 5 21 "VU_"
	calc_SE `indepvar' 6 21 "VU_"
	qui:gen VU_n_flip_`indepvar' = 1
	qui: replace VU_n_flip_`indepvar' = -1 if mat_`indepvar'_beta5<0
	qui:gen `indepvar'_30 = abs(mat_`indepvar'_beta5)
	qui:gen `indepvar'_31 = mat_`indepvar'_beta6*VU_n_flip_`indepvar'
	calc_SE `indepvar' 30 21 "VU_n_"
	calc_SE `indepvar' 31 21 "VU_n_"
	
	* BES comparison models
	qui:gen BES_flip_`indepvar' = 1
	qui:replace BES_flip_`indepvar' = -1 if mat_`indepvar'_beta17<0
	qui:gen `indepvar'_17 = abs(mat_`indepvar'_beta17)
	qui:gen `indepvar'_18 = mat_`indepvar'_beta18*BES_flip_`indepvar'
	qui:gen `indepvar'_19 = mat_`indepvar'_beta19*BES_flip_`indepvar'
	calc_SE `indepvar' 17 8 "BES_"
	calc_SE `indepvar' 18 8 "BES_"
	calc_SE `indepvar' 19 8 "BES_"
	qui:gen BES_n_flip_`indepvar' = 1
	qui: replace BES_n_flip_`indepvar' = -1 if mat_`indepvar'_beta18<0
	qui:gen `indepvar'_36 = abs(mat_`indepvar'_beta18)
	qui:gen `indepvar'_37 = mat_`indepvar'_beta19*VU_n_flip_`indepvar'
	calc_SE `indepvar' 36 8 "BES_n_"
	calc_SE `indepvar' 37 8 "BES_n_"
	
	* DES comparison models
	qui:gen DES_flip_`indepvar' = 1
	qui:replace DES_flip_`indepvar' = -1 if mat_`indepvar'_beta20<0
	qui:gen `indepvar'_20 = abs(mat_`indepvar'_beta20)
	qui:gen `indepvar'_21 = mat_`indepvar'_beta21*DES_flip_`indepvar'
	qui:gen `indepvar'_22 = mat_`indepvar'_beta22*DES_flip_`indepvar'
	calc_SE `indepvar' 20 13 "DES_"
	calc_SE `indepvar' 21 13 "DES_"
	calc_SE `indepvar' 22 13 "DES_"
	qui:gen DES_n_flip_`indepvar' = 1
	qui: replace DES_n_flip_`indepvar' = -1 if mat_`indepvar'_beta21<0
	qui:gen `indepvar'_38 = abs(mat_`indepvar'_beta21)
	qui:gen `indepvar'_39 = mat_`indepvar'_beta22*VU_n_flip_`indepvar'
	calc_SE `indepvar' 38 13 "DES_n_"
	calc_SE `indepvar' 39 13 "DES_n_"
	
	* NES comparison models
	qui:gen NES_flip_`indepvar' = 1
	qui:replace NES_flip_`indepvar' = -1 if mat_`indepvar'_beta23<0
	qui:gen `indepvar'_23 = abs(mat_`indepvar'_beta23)
	qui:gen `indepvar'_24 = mat_`indepvar'_beta24*NES_flip_`indepvar'
	qui:gen `indepvar'_25 = mat_`indepvar'_beta25*NES_flip_`indepvar'
	calc_SE `indepvar' 23 14 "NES_"
	calc_SE `indepvar' 24 14 "NES_"
	calc_SE `indepvar' 25 14 "NES_"
	qui:gen NES_n_flip_`indepvar' = 1
	qui: replace NES_n_flip_`indepvar' = -1 if mat_`indepvar'_beta24<0
	qui:gen `indepvar'_40 = abs(mat_`indepvar'_beta24)
	qui:gen `indepvar'_41 = mat_`indepvar'_beta25*VU_n_flip_`indepvar'
	calc_SE `indepvar' 40 14 "NES_n_"
	calc_SE `indepvar' 41 14 "NES_n_"
	
	* Contact rate robustness
	qui:gen ROBUST_flip_`indepvar' = 1
	qui:replace ROBUST_flip_`indepvar' = -1 if mat_`indepvar'_beta26<0
	qui:gen `indepvar'_26 = abs(mat_`indepvar'_beta26)
	qui:gen `indepvar'_27 = mat_`indepvar'_beta27*ROBUST_flip_`indepvar'
	calc_SE `indepvar' 26 34 "ROBUST_"
	calc_SE `indepvar' 27 34 "ROBUST_"
	
	qui:gen ROBUST_e_flip_`indepvar' =1
	qui: replace ROBUST_e_flip_`indepvar' = -1 if mat_`indepvar'_beta42<0
	qui:gen `indepvar'_42 = abs(mat_`indepvar'_beta42)
	qui:gen `indepvar'_43 = mat_`indepvar'_beta43*ROBUST_e_flip_`indepvar'
	calc_SE `indepvar' 42 34 "ROBUST_e_"
	calc_SE `indepvar' 43 24 "ROBUST_e_"

	* Means, SE, N, delta and t of delta vars
	qui:gen `indepvar'_m = .
	qui:gen `indepvar'_se = .
	qui:gen `indepvar'_Nmin = .
	qui:gen `indepvar'_Nmax = .
	qui:gen `indepvar'_delta = .
	qui:gen `indepvar'_tdelta = .
	forvalues i=1(1)43 {
		qui:egen m_`indepvar'_`i' = mean(`indepvar'_`i')
		qui:replace `indepvar'_m = m_`indepvar'_`i'[1] in `i'
		qui:replace `indepvar'_se = se_`indepvar'_`i'[1] in `i'
		qui:egen Nmin_`indepvar'_`i' = min(mat_`indepvar'_n`i')
		qui:egen Nmax_`indepvar'_`i' = max(mat_`indepvar'_n`i')
		qui:replace `indepvar'_Nmin = Nmin_`indepvar'_`i'[1] in `i'
		qui:replace `indepvar'_Nmax = Nmax_`indepvar'_`i'[1] in `i'
		qui:drop Nmin_`indepvar'_`i' Nmax_`indepvar'_`i'
		qui: drop m_`indepvar'_`i' se_`indepvar'_`i'
	}
	
	* Deltas and t-value of deltas
	qui:replace `indepvar'_delta = `indepvar'_m[2] - `indepvar'_m[1] in 2
	qui:replace `indepvar'_delta = `indepvar'_m[3] - `indepvar'_m[2] in 3
	qui:replace `indepvar'_tdelta = `indepvar'_delta[2]/sqrt(`indepvar'_se[1]^2 + `indepvar'_se[2]^2) in 2
	qui:replace `indepvar'_tdelta = `indepvar'_delta[3]/sqrt(`indepvar'_se[2]^2 + `indepvar'_se[3]^2) in 3
	
	* Deltas and t-value of deltas for winners curse/beta selection models
	qui:replace `indepvar'_delta = `indepvar'_m[8] - `indepvar'_m[7] in 8
	qui:replace `indepvar'_delta = `indepvar'_m[9] - `indepvar'_m[8] in 9
	qui:replace `indepvar'_tdelta = `indepvar'_delta[8]/sqrt(`indepvar'_se[7]^2 + `indepvar'_se[8]^2) in 8
	qui:replace `indepvar'_tdelta = `indepvar'_delta[9]/sqrt(`indepvar'_se[8]^2 + `indepvar'_se[9]^2) in 9
	qui:replace `indepvar'_delta = `indepvar'_m[11] - `indepvar'_m[10] in 11
	qui:replace `indepvar'_tdelta = `indepvar'_delta[11]/sqrt(`indepvar'_se[10]^2 + `indepvar'_se[11]^2) in 11
	qui:replace `indepvar'_delta = `indepvar'_m[13] - `indepvar'_m[12] in 13
	qui:replace `indepvar'_delta = `indepvar'_m[14] - `indepvar'_m[13] in 14
	qui:replace `indepvar'_tdelta = `indepvar'_delta[13]/sqrt(`indepvar'_se[12]^2 + `indepvar'_se[13]^2) in 13
	qui:replace `indepvar'_tdelta = `indepvar'_delta[14]/sqrt(`indepvar'_se[13]^2 + `indepvar'_se[14]^2) in 14
	qui:replace `indepvar'_delta = `indepvar'_m[16] - `indepvar'_m[15] in 16
	qui:replace `indepvar'_tdelta = `indepvar'_delta[16]/sqrt(`indepvar'_se[16]^2 + `indepvar'_se[15]^2) in 16
	
	* Deltas and t-value of deltas for naive renormed models (main, winners curse and beta selection)
	qui:replace `indepvar'_delta = `indepvar'_m[29] - `indepvar'_m[28] in 29
	qui:replace `indepvar'_tdelta = `indepvar'_delta[29]/sqrt(`indepvar'_se[28]^2 + `indepvar'_se[29]^2) in 29
	qui:replace `indepvar'_delta = `indepvar'_m[33] - `indepvar'_m[32] in 33
	qui:replace `indepvar'_tdelta = `indepvar'_delta[33]/sqrt(`indepvar'_se[33]^2 + `indepvar'_se[32]^2) in 33
	qui:replace `indepvar'_delta = `indepvar'_m[35] - `indepvar'_m[34] in 35
	qui:replace `indepvar'_tdelta = `indepvar'_delta[35]/sqrt(`indepvar'_se[34]^2 + `indepvar'_se[35]^2) in 35
	
	* Deltas and t-value of deltas for VU comparison models
	qui:replace `indepvar'_delta = `indepvar'_m[5] - `indepvar'_m[4] in 5
	qui:replace `indepvar'_tdelta = `indepvar'_delta[5]/sqrt(`indepvar'_se[4]^2 + `indepvar'_se[5]^2) in 5
	qui:replace `indepvar'_delta = `indepvar'_m[6] - `indepvar'_m[5] in 6
	qui:replace `indepvar'_tdelta = `indepvar'_delta[6]/sqrt(`indepvar'_se[5]^2 + `indepvar'_se[6]^2) in 6
	qui:replace `indepvar'_delta = `indepvar'_m[31] - `indepvar'_m[30] in 31
	qui:replace `indepvar'_tdelta = `indepvar'_delta[31]/sqrt(`indepvar'_se[30]^2 + `indepvar'_se[31]^2) in 31
	
	* Deltas and t-value of deltas for BES comparison models
	qui:replace `indepvar'_delta = `indepvar'_m[18] - `indepvar'_m[17] in 18
	qui:replace `indepvar'_tdelta = `indepvar'_delta[18]/sqrt(`indepvar'_se[17]^2 + `indepvar'_se[18]^2) in 18
	qui:replace `indepvar'_delta = `indepvar'_m[19] - `indepvar'_m[18] in 19
	qui:replace `indepvar'_tdelta = `indepvar'_delta[19]/sqrt(`indepvar'_se[18]^2 + `indepvar'_se[19]^2) in 19
	qui:replace `indepvar'_delta = `indepvar'_m[37] - `indepvar'_m[36] in 37
	qui:replace `indepvar'_tdelta = `indepvar'_delta[37]/sqrt(`indepvar'_se[36]^2 + `indepvar'_se[37]^2) in 37
	
	* Deltas and t-value of deltas for DES comparison models
	qui:replace `indepvar'_delta = `indepvar'_m[21] - `indepvar'_m[20] in 21
	qui:replace `indepvar'_tdelta = `indepvar'_delta[21]/sqrt(`indepvar'_se[20]^2 + `indepvar'_se[21]^2) in 21
	qui:replace `indepvar'_delta = `indepvar'_m[22] - `indepvar'_m[21] in 22
	qui:replace `indepvar'_tdelta = `indepvar'_delta[22]/sqrt(`indepvar'_se[21]^2 + `indepvar'_se[22]^2) in 22
	qui:replace `indepvar'_delta = `indepvar'_m[39] - `indepvar'_m[38] in 39
	qui:replace `indepvar'_tdelta = `indepvar'_delta[39]/sqrt(`indepvar'_se[38]^2 + `indepvar'_se[39]^2) in 39
	
	* Deltas and t-value of deltas for NES comparison models
	qui:replace `indepvar'_delta = `indepvar'_m[24] - `indepvar'_m[23] in 24
	qui:replace `indepvar'_tdelta = `indepvar'_delta[24]/sqrt(`indepvar'_se[23]^2 + `indepvar'_se[24]^2) in 24
	qui:replace `indepvar'_delta = `indepvar'_m[25] - `indepvar'_m[24] in 25
	qui:replace `indepvar'_tdelta = `indepvar'_delta[25]/sqrt(`indepvar'_se[24]^2 + `indepvar'_se[25]^2) in 25
	qui:replace `indepvar'_delta = `indepvar'_m[41] - `indepvar'_m[40] in 41
	qui:replace `indepvar'_tdelta = `indepvar'_delta[41]/sqrt(`indepvar'_se[40]^2 + `indepvar'_se[41]^2) in 41
	
	* Deltas and t-value of deltas for contact rate robustness models
	qui:replace `indepvar'_delta = `indepvar'_m[27] - `indepvar'_m[26] in 27
	qui:replace `indepvar'_tdelta = `indepvar'_delta[27]/sqrt(`indepvar'_se[26]^2 + `indepvar'_se[27]^2) in 27
	qui:replace `indepvar'_delta = `indepvar'_m[43] - `indepvar'_m[42] in 43
	qui:replace `indepvar'_tdelta = `indepvar'_delta[43]/sqrt(`indepvar'_se[43]^2 + `indepvar'_se[42]^2) in 43
	
	* Some cleanup
	drop mat_`indepvar'_se1*
	drop mat_`indepvar'_var*
	drop mat_`indepvar'_p4-mat_`indepvar'_p50
	drop mat_`indepvar'_n1-mat_`indepvar'_n45
}

* Labeling (necessary for tables and graphs below)
label variable wealth_IHS_m "Gross wealth"
label variable wealth_net_IHS_m "Net wealth"
label variable income10_tr_m "Work income"
label variable education_years_m "Education years"
label variable bin_edu_m "College"
label variable extraversion_SALTY_m "Extraversion"
label variable LOC_SALTY_m "Locus of control"
label variable risk_preference_m "Risk preference"
label variable trust_m "Trust"
label variable antisocial_m "Antisocial att."
label variable altruism_m "Altruism"
label variable utilitarian_m "Utilitarian"
label variable stdIQ_m "IQ"






************************
* RESULTS PRESENTATION *
************************

* Do histograms of all effect size distributions per variable for Online Appendix
set scheme s1mono
gen tempmax=.
local allindepvars = "stdIQ education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian"
foreach indepvar in `allindepvars' {

	* First model comparison (empty to naive)
	qui:su `indepvar'_1
	local m1 = r(mean)
	local max1 = r(max)
	qui:replace tempmax = `max1' in 1
	qui:su `indepvar'_2
	local m2 = r(mean)
	local max2 = r(max)
	qui:replace tempmax = `max2' in 2
	
	* Second model comparison (naive to within-pair)
	qui:su `indepvar'_28
	local m3 = r(mean)
	local max3 = r(max)
	qui:replace tempmax = `max3' in 3
	qui:su `indepvar'_29
	local m4 = r(mean)
	local max4 = r(max)
	qui:replace tempmax = `max4' in 4
	qui:su tempmax
	local max = ceil(r(max)*20)/20
	qui:replace tempmax=.

	* First comparison
	histogram `indepvar'_1, bin(5) xline(0) xline(`m1', lpattern(shortdash)) legend(off) xtitle("Empty") xscale(range(-`max' `max')) xlabel(#20)
	graph save graph1, replace
	histogram `indepvar'_2, bin(5) xline(0) xline(`m2', lpattern(shortdash) lcolor(black)) xline(`m1', lpattern(shortdash) lcolor(red)) legend(off) xtitle("Naive") xscale(range(-`max' `max')) xlabel(#20)
	graph save graph2, replace
	
	* Second comparison
	histogram `indepvar'_28, bin(5) xline(0) xline(`m3', lpattern(shortdash)) legend(off) xtitle("Naive, renormed") xscale(range(-`max' `max')) xlabel(#20)
	graph save graph3, replace
	histogram `indepvar'_29, bin(5) xline(0) xline(`m4', lpattern(shortdash) lcolor(black)) xline(`m3', lpattern(shortdash) lcolor(red)) legend(off) xtitle("Within") xscale(range(-`max' `max')) xlabel(#20)
	graph save graph4, replace
	
	local varlabel : variable label `indepvar'_m
	graph combine graph1.gph graph2.gph graph3.gph graph4.gph, rows(4) title(`varlabel') b2title("Black dashed lines are means; red dashed lines are means from model above")
	graph export "`indepvar'.pdf", replace
}
erase graph1.gph
erase graph2.gph
erase graph3.gph
erase graph4.gph
drop tempmax



* P-curves per variable for full (within-pair) model
local allindepvars = "stdIQ education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	ksmirnov mat_`indepvar'_p3 = mat_`indepvar'_p3
	local ksp = floor(r(p)*1000)/1000
	histogram mat_`indepvar'_p3, title("`varlabel'") xtitle("P-curve, within models. KS test vs. uniform: p =`ksp'")
	graph export "pcurve_`indepvar'.pdf", replace
}




**********************************************
* MAKE MAIN RESULT TABLE FOR ONLINE APPENDIX *
**********************************************

* At this point, we only need to keep the meta-analytical information, so drop all other observations
keep if [_n]<=50

file open table using "table_main.tex", write replace
*file open table using "table_main_alt.tex", write replace //  If using the initial dataset finaldata_alt.dta (with a reduced set of controls), use this line instead of the previous line to produce Table 6 in the online appendix
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\caption{Main results} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|cc|cc|c}" _n
file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Naive, rn} & \textbf{Within} & $\% 1$ & $\% 2$ &\textbf{min-max N} \\ \hline" _n
	
local allindepvars = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[1]
	local se1 : di %12.3f `indepvar'_se[1]
	local c2 : di %12.3f `indepvar'_m[2]
	local se2 : di %12.3f `indepvar'_se[2]
	local c2_2 : di %12.3f `indepvar'_m[28]
	local se2_2 : di %12.3f `indepvar'_se[28]
	local c3 : di %12.3f `indepvar'_m[29]
	local se3 : di %12.3f `indepvar'_se[29]
	local c4 : di %12.1f (100*`indepvar'_m[2]/`indepvar'_m[1])
	local c5 : di %12.1f (100*`indepvar'_m[29]/`indepvar'_m[28])
	local c6 : di `indepvar'_Nmin[1]
	local c7 : di `indepvar'_Nmax[1]
	local t1 = floor(`indepvar'_tdelta[2]*1000)/1000
	local t2 = floor(`indepvar'_tdelta[29]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c2_2' & `c3' & `c4'\% & `c5'\% & `c6'-`c7' \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & ( `se2_2' ) & ( `se3' ) & (t=`t1') & (t=`t2') &  \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Averages of beta coefficients for all 34 political issue variables. Columns \%1 and \%2 are the proportion of the remaining effect size when moving to the next model, in per cent. min-max N is the smallest and largest number of observations across the 34 outcomes per predictor." _n
file write table "\end{table}" _n
file close table




************************************************
* MAKE WINNERS CURSE TABLE FOR ONLINE APPENDIX *
************************************************

local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
local allindepvars = ""
foreach indepvar in `indeplist' {
	if mat_`indepvar'_wc2[1]>=5 {
		local allindepvars = "`allindepvars' `indepvar'"
	}
}

file open table using "table_wc.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\caption{Winners curse results, from naive models} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|c|c}" _n
file write table "\textbf{Predictor} & \textbf{Naive, sig} & \textbf{Within} & $\%$ & $ k$ \\ \hline" _n

foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[32]
	local se1 : di %12.3f `indepvar'_se[32]
	local c2 : di %12.3f `indepvar'_m[33]
	local se2 : di %12.3f `indepvar'_se[33]
	
	local c4 : di %12.1f (100*`indepvar'_m[33]/`indepvar'_m[32])
	
	local k : di mat_`indepvar'_wc2[1]
	local t1 = floor(`indepvar'_tdelta[33]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c4'\% & `k'  \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & (t=`t1') & \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Average beta coefficients for outcomes with \$p<.05$ in naive model. Only predictors with at least 5 included outcomes shown.  The \% column contains the proportion of the remaining effect size when moving to the next model, in per cent. $k$ is the number of outcomes retained." _n
file write table "\end{table}" _n
file close table





***************************************************************
* MAKE BETA SELECTION WINNERS CURSE TABLE FOR ONLINE APPENDIX *
***************************************************************

local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
local allindepvars = ""
foreach indepvar in `indeplist' {
	if mat_`indepvar'_wc4[1]>=5 {
		local allindepvars = "`allindepvars' `indepvar'"
	}
}

file open table using "table_bs.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\caption{Beta selection results, from naive models} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|c|c}" _n
file write table "\textbf{Predictor} & \textbf{Naive, sig} & \textbf{Within} & $\%$ & $ k$ \\ \hline" _n

foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[34]
	local se1 : di %12.3f `indepvar'_se[34]
	local c2 : di %12.3f `indepvar'_m[35]
	local se2 : di %12.3f `indepvar'_se[35]
	
	local c4 : di %12.1f (100*`indepvar'_m[35]/`indepvar'_m[34])
	
	local k : di mat_`indepvar'_wc4[1]
	local t1 = floor(`indepvar'_tdelta[35]*1000)/1000
	
	file write table "`varlabel' & `c1' & `c2'`p1' & `c4'\% & `k'  \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & (t=`t1') & \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Average beta coefficients for outcomes with \$\beta>.1$ in naive model. Only predictors with at least 5 included outcomes shown.  The \% column contains the proportion of the remaining effect size when moving to the next model, in per cent. $k$ is the number of outcomes retained." _n
file write table "\end{table}" _n
file close table





***********************************************************
* REDUCED DIMENSION REGRESSION TABLES FOR ONLINE APPENDIX *
***********************************************************

forvalues i=1(1)5 {
	file open table using "dim`i'.tex", write replace
	file write table "\begin{table}" _n
	file write table "\centering" _n
	file write table "\footnotesize" _n
	file write table "\caption{Reduced dimension `i', raw results} \vspace{0.5cm}" _n
	file write table "\begin{tabular}{l|ccc|c}" _n
	file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Within} & \textbf{N} \\ \hline" _n

	local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
	foreach indepvar in `indeplist' {
		local varlabel : variable label `indepvar'_m
		local b1: di %12.3f mat_`indepvar'_beta44[`i']
		local b2: di %12.3f mat_`indepvar'_beta45[`i']
		local b3: di %12.3f mat_`indepvar'_beta46[`i']
		local se1: di %12.3f mat_`indepvar'_se44[`i']
		local se2: di %12.3f mat_`indepvar'_se45[`i']
		local se3: di %12.3f mat_`indepvar'_se46[`i']
		local n=mat_`indepvar'_n46[`i']
		file write table "`varlabel' & `b1' & `b2' & `b3' & `n' \\" _n
		file write table " & ( `se1' ) & ( `se2' ) & ( `se3' ) \\" _n
	}
	file write table "\hline" _n
	file write table "\end{tabular}" _n _n
	file write table "\vspace{.25cm}" _n _n
	file write table "Regression coefficients per predictor and model. Each predictor run separately. Full set of controls included (see section 4.1.2 in main text)." _n
	file write table "\end{table}" _n
	file close table
}



*****************************************************
* CONTACT RATE ROBUSTNESS TABLE FOR ONLINE APPENDIX *
*****************************************************

local allindepvars = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
file open table using "table_contact.tex", write replace
file write table "\begin{table}[h]" _n
file write table "\centering" _n
file write table "\footnotesize" _n
file write table "\caption{Contact rate robustness check} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|c|cc|c}" _n
file write table "\textbf{Predictor} & \textbf{Within, avg} & \textbf{Within, 0 cnt} & \% \textbf{Empty, avg} & \textbf{Empty, 0 cnt} & \% \\ \hline" _n

foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[26]
	local se1 : di %12.3f `indepvar'_se[26]
	
	local c2 : di %12.3f `indepvar'_m[27]
	local se2 : di %12.3f `indepvar'_se[27]
	
	local c3 : di %12.1f (100*`indepvar'_m[27]/`indepvar'_m[26])
	
	local c4 : di %12.3f `indepvar'_m[42]
	local se4 : di %12.3f `indepvar'_se[42]
	local c5 : di %12.3f `indepvar'_m[43]
	local se5 : di %12.3f `indepvar'_se[43]
	
	local c6 : di %12.1f (100*`indepvar'_m[43]/`indepvar'_m[42])
	
	local t1 = floor(`indepvar'_tdelta[27]*1000)/1000
	local t2 = floor(`indepvar'_tdelta[43]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c3'\% & `c4' & `c5' & `c6'\%  \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & (t=`t1') & ( `se4' ) & ( `se5' ) & (t=`t2')  \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Average beta coefficients with and without conditioning on zero contact. The \% column contains the proportion of the remaining effect size when moving from zero contact to the average effect, in per cent." _n
file write table "\end{table}" _n
file close table




*****************************************
* COMBINED TABLE VU FOR ONLINE APPENDIX *
*****************************************

* Append the estimates from the matched results in the Swedish election study
append using "..\VU_est.dta"
rename VU_educationYears* VU_education_years*
rename VU_workIncome* VU_income10_tr*

file open table using "table_comb_VU.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\footnotesize" _n
file write table "\caption{Comparison results, Swedish election study} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|cc|cc|cc|c}" _n
file write table " & STR & STR & STR & STR & & & SES & SES & \\" _n
file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Naive, rn} & \textbf{Within} & $\% 1$ & $\% 2$ & \textbf{Empty} & \textbf{Naive} & $\Delta$ \\ \hline" _n
	
local allindepvars = "education_years bin_edu income10_tr"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[4]
	local se1 : di %12.3f `indepvar'_se[4]
	local c2 : di %12.3f `indepvar'_m[5]
	local se2 : di %12.3f `indepvar'_se[5]
	local c2_2 : di %12.3f `indepvar'_m[30]
	local se2_2 : di %12.2f `indepvar'_se[30]
	local c3 : di %12.3f `indepvar'_m[31]
	local se3 : di %12.3f `indepvar'_se[31]
	
	local c4 : di %12.3f VU_`indepvar'_m[51]
	local se4 : di %12.3f VU_`indepvar'_se[51]
	local c5 : di %12.3f VU_`indepvar'_m[52]
	local se5 : di %12.3f VU_`indepvar'_se[52]
	local d1 : di %12.1f (100*`indepvar'_m[5]/`indepvar'_m[4])
	local d2 : di %12.1f (100*`indepvar'_m[31]/`indepvar'_m[30])
	local d3 : di %12.1f (100*VU_`indepvar'_m[52]/VU_`indepvar'_m[51])
	
	local t1=floor(`indepvar'_tdelta[5]*1000)/1000
	local t2=floor(`indepvar'_tdelta[31]*1000)/1000
	local t3=floor(VU_`indepvar'_tdelta[52]*1000)/1000
	
	file write table "`varlabel' & `c1' & `c2' & `c2_2' & `c3' & `d1'\% & `d2'\% & `c4' & `c5' & `d3'\% \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & ( `se2_2' ) & ( `se3' ) & (t=`t1') & (t=`t2') & ( `se4' ) & ( `se5' ) & (t=`t3') \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Averages of beta coefficients for 21 shared political issue variables." _n
file write table "\end{table}" _n
file close table





******************************************
* COMBINED TABLE BES FOR ONLINE APPENDIX *
******************************************

* Append the estimates from the matched results in the British election study
append using "..\BES_est.dta"
rename BES_educationYears* BES_education_years*
rename BES_income* BES_income10_tr*

file open table using "table_comb_BES.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\footnotesize" _n
file write table "\caption{Comparison results, British election study} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|cc|cc|cc|c}" _n
file write table " & STR & STR & STR & STR & & & BES & BES & \\" _n
file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Naive, rn} & \textbf{Within} & $\% 1$ & $\% 2$ & \textbf{Empty} & \textbf{Naive} & $\Delta$ \\ \hline" _n

local allindepvars = "education_years bin_edu income10_tr"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[17]
	local se1 : di %12.3f `indepvar'_se[17]
	local c2 : di %12.3f `indepvar'_m[18]
	local se2 : di %12.3f `indepvar'_se[18]
	local c2_2: di %12.3f `indepvar'_m[36]
	local se2_2 : di %12.3f `indepvar'_se[36]
	local c3 : di %12.3f `indepvar'_m[37]
	local se3 : di %12.3f `indepvar'_se[37]
	
	local c4 : di %12.3f BES_`indepvar'_m[53]
	local se4 : di %12.3f BES_`indepvar'_se[53]
	local c5 : di %12.3f BES_`indepvar'_m[54]
	local se5 : di %12.3f BES_`indepvar'_se[54]
	local d1 : di %12.1f (100*`indepvar'_m[18]/`indepvar'_m[17])
	local d2 : di %12.1f (100*`indepvar'_m[37]/`indepvar'_m[36])
	local d3 : di %12.1f (100*BES_`indepvar'_m[54]/BES_`indepvar'_m[53])
	
	local t1=floor(`indepvar'_tdelta[18]*1000)/1000
	local t2=floor(`indepvar'_tdelta[37]*1000)/1000
	local t3=floor(BES_`indepvar'_tdelta[54]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c2_2' & `c3' & `d1'\% & `d2'\% & `c4' & `c5' & `d3'\% \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & ( `se2_2' ) & ( `se3' ) & (t=`t1') & (t=`t2') & ( `se4' ) & ( `se5' ) & (t=`t3') \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Averages of beta coefficients for 8 shared political issue variables (standardized by positive sign in the empty model)." _n
file write table "\end{table}" _n
file close table



******************************************
* COMBINED TABLE DES FOR ONLINE APPENDIX *
******************************************

* Append the estimates from the matched results in the Danish election study
append using "..\DES_est.dta"
rename DES_educationYears* DES_education_years*
rename DES_income* DES_income10_tr*

file open table using "table_comb_DES.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\footnotesize" _n
file write table "\caption{Comparison results, Danish election study} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|cc|cc|cc|c}" _n
file write table " & STR & STR & STR & STR & & & DES & DES & \\" _n
file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Naive, rn} & \textbf{Within} & $\% 1$ & $\% 2$ & \textbf{Empty} & \textbf{Naive} & $\Delta$ \\ \hline" _n
	
local allindepvars = "education_years bin_edu income10_tr trust"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[20]
	local se1 : di %12.3f `indepvar'_se[20]
	local c2 : di %12.3f `indepvar'_m[21]
	local se2 : di %12.3f `indepvar'_se[21]
	local c2_2 : di %12.3f `indepvar'_m[38]
	local se2_2 : di %12.3f `indepvar'_se[38]
	local c3 : di %12.3f `indepvar'_m[39]
	local se3 : di %12.3f `indepvar'_se[39]
	
	local c4 : di %12.3f DES_`indepvar'_m[55]
	local se4 : di %12.3f DES_`indepvar'_se[55]
	local c5 : di %12.3f DES_`indepvar'_m[56]
	local se5 : di %12.3f DES_`indepvar'_se[56]
	local d1 : di %12.1f (100*`indepvar'_m[21]/`indepvar'_m[20])
	local d2 : di %12.1f (100*`indepvar'_m[39]/`indepvar'_m[38])
	local d3 : di %12.1f (100*DES_`indepvar'_m[56]/DES_`indepvar'_m[55])
	
	local t1=floor(`indepvar'_tdelta[21]*1000)/1000
	local t2=floor(`indepvar'_tdelta[39]*1000)/1000
	local t3=floor(DES_`indepvar'_tdelta[56]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c2_2' & `c3' & `d1'\% & `d2'\% & `c4' & `c5' & `d3'\% \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & ( `se2_2' ) & ( `se3' ) & (t=`t1') & (t=`t2') & ( `se4' ) & ( `se5' ) & (t=`t3') \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Averages of beta coefficients for 13 shared political issue variables (standardized by positive sign in the empty model)." _n
file write table "\end{table}" _n
file close table



******************************************
* COMBINED TABLE NES FOR ONLINE APPENDIX *
******************************************

* Append the estimates from the results in the Norwegian election study
append using "..\NES_est.dta"
rename NES_educationYears* NES_education_years*
rename NES_income* NES_income10_tr*

file open table using "table_comb_NES.tex", write replace
file write table "\begin{table}" _n
file write table "\centering" _n
file write table "\footnotesize" _n
file write table "\caption{Comparison results, Norwegian election study} \vspace{0.5cm}" _n
file write table "\begin{tabular}{l|cc|cc|cc|cc|c}" _n
file write table " & STR & STR & STR & STR & & & NES & NES & \\" _n
file write table "\textbf{Predictor} & \textbf{Empty} & \textbf{Naive} & \textbf{Naive, rn} & \textbf{Within} & $\% 1$ & $\% 2$ & \textbf{Empty} & \textbf{Naive} & $\Delta$ \\ \hline" _n
	
local allindepvars = "education_years bin_edu income10_tr"
foreach indepvar in `allindepvars' {
	local varlabel : variable label `indepvar'_m
	local c1 : di %12.3f `indepvar'_m[23]
	local se1 : di %12.3f `indepvar'_se[23]
	local c2 : di %12.3f `indepvar'_m[24]
	local se2 : di %12.3f `indepvar'_se[24]
	local c2_2 : di %12.3f `indepvar'_m[40]
	local se2_2 : di %12.3f `indepvar'_se[40]
	local c3 : di %12.3f `indepvar'_m[41]
	local se3 : di %12.3f `indepvar'_se[41]
	
	local c4 : di %12.3f NES_`indepvar'_m[57]
	local se4 : di %12.3f NES_`indepvar'_se[57]
	local c5 : di %12.3f NES_`indepvar'_m[58]
	local se5 : di %12.3f NES_`indepvar'_se[58]
	local d1 : di %12.1f (100*`indepvar'_m[24]/`indepvar'_m[23])
	local d2 : di %12.1f (100*`indepvar'_m[41]/`indepvar'_m[40])
	local d3 : di %12.1f (100*NES_`indepvar'_m[58]/NES_`indepvar'_m[57])

	local t1=floor(`indepvar'_tdelta[24]*1000)/1000
	local t2=floor(`indepvar'_tdelta[41]*1000)/1000
	local t3=floor(NES_`indepvar'_tdelta[58]*1000)/1000

	file write table "`varlabel' & `c1' & `c2' & `c2_2' & `c3' & `d1'\% & `d2'\% & `c4' & `c5' & `d3'\% \\" _n
	file write table " & ( `se1' ) & ( `se2' ) & ( `se2_2' ) & ( `se3' ) & (t=`t1') & (t=`t2') & ( `se4' ) & ( `se5' ) & (t=`t3') \\" _n
}

file write table "\hline" _n
file write table "\end{tabular}" _n _n
file write table "\vspace{.25cm}" _n _n
file write table "Averages of beta coefficients for 14 shared political issue variables (standardized by positive sign in the empty model)." _n
file write table "\end{table}" _n
file close table





*****************************
* MAIN GRAPHS FOR THE PAPER *
*****************************

set scheme s1mono
keep *_m *_se *_wc*


* First transform data and get 90% CI's.
* Statas graphing functions are a bit clunky,
* so this piece of code is rather convoluted.
gen ORDER=_n
gen empty=.
gen naive=.
gen naive_rn=.
gen within=.

gen lower_empty=.
gen upper_empty=.
gen lower_naive=.
gen upper_naive=.
gen lower_naive_rn=.
gen upper_naive_rn=.
gen lower_within=.
gen upper_within=.

gen predictor=""
local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
local i=1
foreach indepvar in `indeplist' {
	replace empty=`indepvar'_m[1] in `i'
	replace naive=`indepvar'_m[2] in `i'
	replace naive_rn=`indepvar'_m[28] in `i'
	replace within=`indepvar'_m[29] in `i'
	
	replace upper_empty=`indepvar'_m[1]+`indepvar'_se[1]*1.65 in `i'
	replace lower_empty=`indepvar'_m[1]-`indepvar'_se[1]*1.65 in `i'
	replace upper_naive=`indepvar'_m[2]+`indepvar'_se[2]*1.65 in `i'
	replace lower_naive=`indepvar'_m[2]-`indepvar'_se[2]*1.65 in `i'
	replace upper_naive_rn=`indepvar'_m[28]+`indepvar'_se[28]*1.65 in `i'
	replace lower_naive_rn=`indepvar'_m[28]-`indepvar'_se[28]*1.65 in `i'
	replace upper_within=`indepvar'_m[29]+`indepvar'_se[29]*1.65 in `i'
	replace lower_within=`indepvar'_m[29]-`indepvar'_se[29]*1.65 in `i'
	
	local varlabel : variable label `indepvar'_m
	replace predictor="`varlabel'" in `i'
	local i=`i'+1
	
	
}
sort predictor
gen n=_n
expand 4
bysort n: gen origin=_n
gen effect=.
gen upper=.
gen lower=.
sort predictor
by predictor: gen model=_n
replace effect=empty if model==1
replace effect=naive if model==2
replace effect=naive_rn if model==3
replace effect=within if model==4
replace upper=upper_empty if model==1
replace upper=upper_naive if model==2
replace upper=upper_naive_rn if model==3
replace upper=upper_within if model==4
replace lower=lower_empty if model==1
replace lower=lower_naive if model==2
replace lower=lower_naive_rn if model==3
replace lower=lower_within if model==4

* Graph of empty versus naive effects
preserve
	keep if predictor!=""
	drop if model==4
	gen nn=_n
	twoway (bar effect nn if model==1, fcolor(gs14) lcolor(black)) ///
			(bar effect nn if model==2, fcolor(gs10) lcolor(black)) ///
			(rcap upper lower nn if model==1, lcolor(black) lwidth(thin)) ///
			(rcap upper lower nn if model==2, lcolor(black) lwidth(thin)), ///		
			legend(row(1)region(lwidth(none)) order(1 "Empty" 2 "Naive")) ///
			xlabel(1.5 "Altruism" 4.5 "Antisocial att." 7.5 "College" 10.5 "Education years" 13.5 "Extraversion" 16.5 "Gross wealth" 19.5 "IQ" 22.5 "Locus of control" 25.5 "Net wealth" 28.5 "Risk preference" 31.5 "Trust" 34.5 "Utilitarian" 37.5 "Work income", angle(45) tlength(0)) ///
			ylabel(0(.05).1,grid) ytitle("Effect size") xtitle("")
	graph export "figure1.pdf", replace
restore

* Graph of naive versus within effects
preserve
	keep if predictor!=""
	drop if model==1
	gen nn=_n
	twoway (bar effect nn if model==3, fcolor(gs8) lcolor(black)) ///
			(bar effect nn if model==4, fcolor(gs5) lcolor(black)) ///
			(rcap upper lower nn if model==3, lcolor(black) lwidth(thin)) ///
			(rcap upper lower nn if model==4, lcolor(black) lwidth(thin)), ///		
			legend(row(1)region(lwidth(none)) order(1 "Naive, renormed" 2 "Within")) ///
			xlabel(2.5 "Altruism" 5.5 "Antisocial att." 8.5 "College" 11.5 "Education years" 14.5 "Extraversion" 17.5 "Gross wealth" 20.5 "IQ" 23.5 "Locus of control" 26.5 "Net wealth" 29.5 "Risk preference" 32.5 "Trust" 35.5 "Utilitarian" 38.5 "Work income", angle(45) tlength(0)) ///
			ylabel(0(.02).1,grid) ytitle("Effect size") xtitle("")
	graph export "figure2.pdf", replace
restore



* Redo entire process for Winners curse graph
keep if origin==1
sort ORDER
replace model=.
replace naive_rn=.
replace within=.
replace upper_naive_rn=.
replace lower_naive_rn=.
replace upper_within=.
replace lower_within=.

replace predictor=""
local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
local allindepvars = ""
foreach indepvar in `indeplist' {
	if mat_`indepvar'_wc2[1]>=5 {
		local allindepvars = "`allindepvars' `indepvar'"
	}
}
local i=1
foreach indepvar in `allindepvars' {
	replace naive_rn=`indepvar'_m[32] in `i'
	replace within=`indepvar'_m[33] in `i'
	
	replace upper_naive_rn=`indepvar'_m[32]+`indepvar'_se[32]*1.65 in `i'
	replace lower_naive_rn=`indepvar'_m[32]-`indepvar'_se[32]*1.65 in `i'
	replace upper_within=`indepvar'_m[33]+`indepvar'_se[33]*1.65 in `i'
	replace lower_within=`indepvar'_m[33]-`indepvar'_se[33]*1.65 in `i'
	
	local varlabel : variable label `indepvar'_m
	replace predictor="`varlabel'" in `i'
	
	replace mat_`indepvar'_wc2 = mat_`indepvar'_wc2[1]
	local i=`i'+1
}
sort predictor
replace n=_n
expand 3
bysort n: replace origin=_n
replace effect=.
replace upper=.
replace lower=.
sort predictor
by predictor: replace model=_n
replace effect=naive_rn if model==1
replace effect=within if model==2
replace upper=upper_naive_rn if model==1
replace upper=upper_within if model==2
replace lower=lower_naive_rn if model==1
replace lower=lower_within if model==2

preserve
	keep if predictor!=""
	gen nn=_n
	local wc1=mat_altruism_wc2[1]
	local wc2=mat_antisocial_wc2[1]
	local wc3=mat_bin_edu_wc2[1]
	local wc4=mat_education_years_wc2[1]
	local wc5=mat_extraversion_SALTY_wc2[1]
	local wc6=mat_wealth_IHS_wc2[1]
	local wc7=mat_stdIQ_wc2[1]
	local wc8=mat_LOC_SALTY_wc2[1]
	local wc9=mat_wealth_net_IHS_wc2[1]
	local wc10=mat_risk_preference_wc2[1]
	local wc11=mat_trust_wc2[1]
	local wc12=mat_income10_tr_wc2[1]
	twoway (bar effect nn if model==1, fcolor(gs8) lcolor(black)) ///
			(bar effect nn if model==2, fcolor(gs5) lcolor(black)) ///
			(rcap upper lower nn if model==1, lcolor(black) lwidth(thin)) ///
			(rcap upper lower nn if model==2, lcolor(black) lwidth(thin)), ///		
			legend(row(1)region(lwidth(none)) order(1 "Naive, renormed" 2 "Within")) ///
			xlabel(1.5 "Altruism (`wc1')" 4.5 "Antisocial att. (`wc2')" 7.5 "College (`wc3')" 10.5 "Education years (`wc4')" 13.5 "Extraversion (`wc5')" 16.5 "Gross wealth (`wc6')" 19.5 "IQ (`wc7')" 22.5 "Locus of control (`wc8')" 25.5 "Net wealth (`wc9')" 28.5 "Risk preference (`wc10')" 31.5 "Trust (`wc11')" 34.5 "Work income (`wc12')", angle(45) tlength(0)) ///
			ylabel(0(.05).2,grid) ytitle("Effect size") xtitle("")
	graph export "figure3.pdf", replace
restore




* Redo entire process for Beta selection graph
keep if origin==1
sort ORDER
replace model=.
replace naive_rn=.
replace within=.
replace upper_naive_rn=.
replace lower_naive_rn=.
replace upper_within=.
replace lower_within=.

replace predictor=""
local indeplist = "education_years bin_edu wealth_IHS wealth_net_IHS income10_tr trust extraversion_SALTY LOC_SALTY risk_preference antisocial altruism utilitarian stdIQ"
local allindepvars = ""
foreach indepvar in `indeplist' {
	if mat_`indepvar'_wc4[1]>=5 {
		local allindepvars = "`allindepvars' `indepvar'"
	}
}
local i=1
foreach indepvar in `allindepvars' {
	replace naive_rn=`indepvar'_m[34] in `i'
	replace within=`indepvar'_m[35] in `i'
	
	replace upper_naive_rn=`indepvar'_m[34]+`indepvar'_se[34]*1.65 in `i'
	replace lower_naive_rn=`indepvar'_m[34]-`indepvar'_se[34]*1.65 in `i'
	replace upper_within=`indepvar'_m[35]+`indepvar'_se[35]*1.65 in `i'
	replace lower_within=`indepvar'_m[35]-`indepvar'_se[35]*1.65 in `i'
	
	local varlabel : variable label `indepvar'_m
	replace predictor="`varlabel'" in `i'
	
	replace mat_`indepvar'_wc4 = mat_`indepvar'_wc4[1]
	local i=`i'+1
}
sort predictor
replace n=_n
expand 3
bysort n: replace origin=_n
replace effect=.
replace upper=.
replace lower=.
sort predictor
by predictor: replace model=_n
replace effect=naive_rn if model==1
replace effect=within if model==2
replace upper=upper_naive_rn if model==1
replace upper=upper_within if model==2
replace lower=lower_naive_rn if model==1
replace lower=lower_within if model==2

preserve
	keep if predictor!=""
	gen nn=_n
	local wc1=mat_antisocial_wc4[1]
	local wc2=mat_bin_edu_wc4[1]
	local wc3=mat_education_years_wc4[1]
	local wc4=mat_wealth_IHS_wc4[1]
	local wc5=mat_stdIQ_wc4[1]
	local wc6=mat_wealth_net_IHS_wc4[1]
	local wc7=mat_risk_preference_wc4[1]
	local wc8=mat_trust_wc4[1]
	twoway (bar effect nn if model==1, fcolor(gs8) lcolor(black)) ///
			(bar effect nn if model==2, fcolor(gs5) lcolor(black)) ///
			(rcap upper lower nn if model==1, lcolor(black) lwidth(thin)) ///
			(rcap upper lower nn if model==2, lcolor(black) lwidth(thin)), ///		
			legend(row(1)region(lwidth(none)) order(1 "Naive, renormed" 2 "Within")) ///
			xlabel(1.5 "Antisocial att. (`wc1')" 4.5 "College (`wc2')" 7.5 "Education years (`wc3')" 10.5 "Gross wealth (`wc4')" 13.5 "IQ (`wc5')" 16.5 "Net wealth (`wc6')" 19.5 "Risk preference (`wc7')" 22.5 "Trust (`wc8')", angle(45) tlength(0)) ///
			ylabel(0(.05).2,grid) ytitle("Effect size") xtitle("")
	graph export "figure4.pdf", replace
restore

